﻿using PigMonica.Common.Utils;
using PigMonica.Common.Utils.Pig;
using PigMonica.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PigMonica.Home
{
    public partial class Home : Form
    {
        private string str = "";
        private int count_old = 0;
        private int count_new = 0;
        ZBJViewModel model;
        ZBJNewViewModel newmodel;
        public Home()
        {
            model = new ZBJViewModel();
            newmodel = new ZBJNewViewModel();
            InitializeComponent();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            timer2.Stop();
            this.Hide();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            Tick();

            if (count_new > count_old)
            {
                count_old = count_new;
                var minus = count_new - count_old;
                DialogResult dr = MessageBox.Show("有" + minus + "个新的任务，去看看？\n（确定跳转，取消继续监听）", "提示", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    //用户选择确认的操作
                    System.Diagnostics.Process.Start(str);
                }
                else if (dr == DialogResult.Cancel)
                {
                    //用户选择取消的操作
                }
               
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Tick2();
            timer2.Enabled = true;
            timer2.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            count_old = 0;
            str = CommonUrl.wzjs;
            timer1.Enabled = true;
            timer1.Start();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            count_old = 0;
            str = CommonUrl.webkf;
            timer1.Enabled = true;
            timer1.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            count_old = 0;
            str = CommonUrl.website;
            timer1.Enabled = true;
            timer1.Start();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            count_old = 0;
            str = CommonUrl.web;
            timer1.Enabled = true;
            timer1.Start();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            count_old = 0;
            str = CommonUrl.cswh;
            timer1.Enabled = true;
            timer1.Start();
        }

        private void Tick()
        {
            CommonUtils cu = new CommonUtils();
            count_new = cu.GetContentFromUrl(str);
        }

        private void Tick2()
        {
            
            CommonUtils cu = new CommonUtils();
            newmodel.WzjsCount = cu.GetContentFromUrl(CommonUrl.wzjs);
            newmodel.WebkfCount = cu.GetContentFromUrl(CommonUrl.webkf);
            newmodel.WebsiteCount = cu.GetContentFromUrl(CommonUrl.website);
            newmodel.WebCount = cu.GetContentFromUrl(CommonUrl.web);
            newmodel.CswhCount = cu.GetContentFromUrl(CommonUrl.cswh);
            var wzjs_minus = newmodel.WzjsCount - model.WzjsCount;
            var webkf_minus = newmodel.WebkfCount - model.WebkfCount;
            var website_minus = newmodel.WebsiteCount - model.WebsiteCount;
            var web_minus = newmodel.WebCount - model.WebCount;
            var cswh_minus = newmodel.CswhCount - model.CswhCount;

            if (wzjs_minus > 0 &&  model.WzjsCount != 0)
            {

                
                DialogResult dr = MessageBox.Show("有" + wzjs_minus + "个新的任务，去看看？\n（确定跳转，取消继续监听）", "提示", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    //用户选择确认的操作
                    System.Diagnostics.Process.Start(CommonUrl.wzjs);
                }
                else if (dr == DialogResult.Cancel)
                {
                    //用户选择取消的操作
                }
            }
            if (webkf_minus > 0 && model.WebkfCount != 0)
            {
              
                DialogResult dr = MessageBox.Show("有" + wzjs_minus + "个新的任务，去看看？\n（确定跳转，取消继续监听）", "提示", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    //用户选择确认的操作
                    System.Diagnostics.Process.Start(CommonUrl.webkf);
                }
                else if (dr == DialogResult.Cancel)
                {
                    //用户选择取消的操作
                }
            }
            if (website_minus > 0 && model.WebsiteCount != 0)
            {
                DialogResult dr = MessageBox.Show("有" + website_minus + "个新的任务，去看看？\n（确定跳转，取消继续监听）", "提示", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    //用户选择确认的操作
                    System.Diagnostics.Process.Start(CommonUrl.website);
                }
                else if (dr == DialogResult.Cancel)
                {
                    //用户选择取消的操作
                }
            }
            if (web_minus > 0 && model.WebCount != 0)
            {
                DialogResult dr = MessageBox.Show("有" + web_minus + "个新的任务，去看看？\n（确定跳转，取消继续监听）", "提示", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    //用户选择确认的操作
                    System.Diagnostics.Process.Start(CommonUrl.web);
                }
                else if (dr == DialogResult.Cancel)
                {
                    //用户选择取消的操作
                }
            }
            if (cswh_minus > 0 && model.CswhCount != 0)
            {
                DialogResult dr = MessageBox.Show("有" + cswh_minus + "个新的任务，去看看？\n（确定跳转，取消继续监听）", "提示", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    //用户选择确认的操作
                    System.Diagnostics.Process.Start(CommonUrl.cswh);
                }
                else if (dr == DialogResult.Cancel)
                {
                    //用户选择取消的操作
                }
            }
            model.WzjsCount = newmodel.WzjsCount;
            model.WebkfCount = newmodel.WebkfCount;
            model.WebsiteCount = newmodel.WebsiteCount;
            model.WebCount = newmodel.WebCount;
            model.CswhCount = newmodel.CswhCount;
                   
            label1.Text = "定制任务：" + model.WzjsCount + "  \t" +
                        "前端任务：" + model.WebkfCount + "  \t" +
                        "模板任务：" + model.WebsiteCount + "  \t" +
                        "二次任务：" + model.WebCount + "  \t" +
                        "测试任务：" + model.CswhCount + "  \t";
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            Tick2();
           
        }

        private void button7_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            timer2.Stop();
        }
    }
}
